﻿#include <iostream>
#include <vector>

using namespace std;

// 前向声明函数
vector<double> generate_signal(double frequency, double amplitude, double sampling_rate, int samples);
vector<double> combine_signals(const vector<vector<double>>& signals);
vector<pair<double, double>> perform_fft(const vector<double>& signal, double sampling_rate);
vector<double> apply_fir_filter(const vector<double>& signal, const vector<double>& coefficients);
vector<double> apply_iir_filter(const vector<double>& signal, const vector<double>& b_coefficients, const vector<double>& a_coefficients);

int main() {
    // 参数设置
    double sampling_rate = 1000.0;  // 采样率：1000Hz
    int samples = 1000;  // 总采样点数

    // 生成信号
    auto signal1 = generate_signal(50, 1.0, sampling_rate, samples);
    auto signal2 = generate_signal(150, 0.5, sampling_rate, samples);
    auto combined_signal = combine_signals({ signal1, signal2 });

    // 输出合成信号（时域）
    cout << "Time Domain Signal (Sample Index, Amplitude):\n";
    for (int i = 0; i < combined_signal.size(); ++i) {
        cout << i << "\t" << combined_signal[i] << "\n";
    }

    // 执行FFT并输出频谱
    auto spectrum = perform_fft(combined_signal, sampling_rate);
    cout << "\nFrequency Spectrum (Frequency, Magnitude):\n";
    for (const auto& [freq, mag] : spectrum) {
        cout << freq << "\t" << mag << "\n";
    }

    // 定义FIR滤波器系数
    vector<double> fir_coefficients = { 0.2, 0.2, 0.2, 0.2, 0.2 }; // 例子中的 FIR 滤波器系数
    auto filtered_signal_fir = apply_fir_filter(combined_signal, fir_coefficients);

    // 输出 FIR 滤波后的信号（时域）
    cout << "\nFiltered Signal (FIR) (Sample Index, Amplitude):\n";
    for (int i = 0; i < filtered_signal_fir.size(); ++i) {
        cout << i << "\t" << filtered_signal_fir[i] << "\n";
    }

    // 定义IIR滤波器的系数
    vector<double> b_coefficients = { 0.1, 0.1, 0.1 };  // 示例分子系数
    vector<double> a_coefficients = { 1, -0.8 };         // 示例分母系数
    auto filtered_signal_iir = apply_iir_filter(combined_signal, b_coefficients, a_coefficients);

    // 输出 IIR 滤波后的信号（时域）
    cout << "\nFiltered Signal (IIR) (Sample Index, Amplitude):\n";
    for (int i = 0; i < filtered_signal_iir.size(); ++i) {
        cout << i << "\t" << filtered_signal_iir[i] << "\n";
    }

    return 0;
}